package com.hlf.modules.merchant.table.service.impl;

import com.hlf.common.constants.Constants;
import com.hlf.common.exception.BusinessException;
import com.hlf.common.utils.PageUtils;
import com.hlf.common.utils.Query;
import com.hlf.modules.api.interceptor.AuthorizationInterceptor;
import com.hlf.modules.merchant.table.entity.TableType;
import com.hlf.modules.merchant.table.mapper.TableTypeMapper;
import com.hlf.modules.merchant.table.service.TableService;
import com.hlf.modules.merchant.table.service.TableTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 餐桌类型
 * Created by Administrator on 2017/9/26.
 */
@Service
public class TableTypeServiceImpl implements TableTypeService {

    @Autowired
    private TableTypeMapper tableTypeMapper;
    @Autowired
    private TableService tableService;

    /**
     * 查本店餐桌类型列表
     * 逻辑显示的类型列表
     *
     * @param storeId
     * @return
     */
    @Override
    public List<TableType> queryListForStoreId(Long storeId) {
        return tableTypeMapper.queryListForStoreId(storeId);
    }

    /**
     * 添加餐桌类型
     *
     * @param tableType
     */
    @Override
    public Integer insertSelective(TableType tableType) {
        return tableTypeMapper.insertSelective(tableType);
    }

    /**
     * 查询单个餐桌类型
     *
     * @param id
     * @return
     */
    @Override
    public TableType queryObjectForId(long id) {
        return tableTypeMapper.queryObject(id);
    }

    /**
     * 修改餐桌类型
     *
     * @param tableType
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(TableType tableType) {
        return tableTypeMapper.updateByPrimaryKeySelective(tableType);
    }


    /**
     * 神丢查询餐桌类型
     *
     * @param id
     * @return
     */
    @Override
    public List<TableType> queryDepthListForStoreId(long id) {
        return tableTypeMapper.queryDepthListForStoreId(id);
    }

    /**
     * 删除餐桌类型
     *
     * @param request
     * @param id
     */
    @Override
    public void deleteTableType(HttpServletRequest request, String id) {
        int num = tableService.queryCountForTableTypeId(Long.parseLong(id));
        if (num != 0) {
            throw new BusinessException("当前餐桌类型下还有绑定餐桌,请解除关联后再删除!");
        }

        Long userId = (Long) request.getAttribute(AuthorizationInterceptor.LOGIN_USER_KEY);
        TableType tableType = new TableType();
        tableType.setId(Long.parseLong(id));
        tableType.setLastUpdateBy(userId);
        tableType.setDelFlag(Constants.DELETE_FLAG_UNABLE);

        int i = updateByPrimaryKeySelective(tableType);
        if (i == 0) {
            throw new BusinessException("删除失败!");
        }
    }

    @Override
    public PageUtils queryListForPage(Map<String, Object> params) {
        Query query = new Query(params);
        int total = tableTypeMapper.queryTotal(query);
        List<TableType> tableList = null;
        if (total > 0) {
            tableList = tableTypeMapper.queryList(query);
        }
        return new PageUtils(tableList, total, query.getLimit(), query.getPage());
    }


}
